package com.zhf.dao.impl;

import com.zhf.dao.StudentDao;
import com.zhf.entity.Student;
import com.zhf.utils.ConnectionUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public int queryCount() {
        int result = 0;
        List<Integer> list = new ArrayList<>();
        String sql = "select count(1) from student";
        ConnectionUtil.DQL(sql, new Object[]{}, (rs -> {
            try{
                if(rs.next()){
                    int res =rs.getInt(1);
                    list.add(res);
                }
            }
            catch (SQLException e){
                e.printStackTrace();
            }
        }));
        if(list.size() == 1){
            result =list.get(0);
        }
        return result;
    }

    @Override
    public List<Student> queryAllStudent(int begin, int size) {
        String sql = "select id,name,gender,birthDay,email,remark\n" +
                "from student limit ?, ?";
        List<Student> students = fromResultGetList(begin, size, sql);
        return students;
    }

    @Override
    public Student queryStudentById(String id) {
        String sql = "select id,name,gender,birthDay,email,remark\n" +
                "from student " +
                "where id = ?";
        List<Student> students = new ArrayList<>();
        ConnectionUtil.DQL(sql, new Object[]{id}, rs -> {
            try{
                if (rs.next()){
                    Student student = new Student();
                    tableToPojo(student, rs, new Integer[]{1,2,3,4,5,6});
                    students.add(student);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        if(students != null && students.size() > 0){
            return students.get(0);
        }
        else{
            return null;
        }
    }

    @Override
    public boolean insertStudent(Student student) {
        String sql = "insert into student (id, name, gender, birthDay,email,remark) values(?,?,?,?,?,?)";
        int rowId = -1;
        rowId = ConnectionUtil.DML(sql, new Object[]{student.getId(), student.getName(), student.getGender(), student.getBirthDay(), student.getEmail(), student.getRemark()});
        if(rowId > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStudent(Student student) {
        String sql = "update student set name = ?, gender = ?, birthDay = ?, email = ?, remark = ? where id = ?";
        int rowId = -1;
        rowId = ConnectionUtil.DML(sql, new Object[]{student.getName(), student.getGender(), student.getBirthDay(), student.getEmail(), student.getRemark(), student.getId()});
        if(rowId > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<Student> queryStudentByIds(String[] studentIds) {
        String sql = "select id,name,gender,birthDay,email,remark from student where id in (";
        StringBuffer sb = new StringBuffer();
        if(studentIds != null && studentIds.length > 0){
            for (String studentId : studentIds){
                sb.append("?,");
            }
        }
        if(sb.length() > 1){
            sql = sql + sb.substring(0, sb.length() - 1);
        }
        sql = sql + ")";
        List<Student> students = new ArrayList<>();
        System.out.println(studentIds);
        System.out.println(sql);

        ConnectionUtil.DQL(sql, studentIds, rs -> {
            try{
                while (rs.next()){
                    Student student = new Student();
                    tableToPojo(student, rs, new Integer[]{1,2,3,4,5,6});
                    students.add(student);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        return students;
    }

    @Override
    public List<Student> queryStudentByClassNos(String[] classNos) {
        String sql = "select id,name,gender,birthDay,email,remark from student where class_no in (";
        StringBuffer sb = new StringBuffer();
        if(classNos != null && classNos.length > 0){
            for (String classNo : classNos){
                sb.append("?,");
            }
        }
        if(sb.length() > 1){
            sql = sql + sb.substring(0, sb.length() - 1);
        }
        sql = sql + ")";
        List<Student> students = new ArrayList<>();

        ConnectionUtil.DQL(sql, classNos, rs -> {
            try{
                while (rs.next()){
                    Student student = new Student();
                    tableToPojo(student, rs, new Integer[]{1,2,3,4,5,6});
                    students.add(student);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        return students;
    }

    @Override
    public int deleteStudentByIds(String[] studentIds) {
        String sql = "delete from student where id in (";
        StringBuffer sb = new StringBuffer();
        if(studentIds != null && studentIds.length > 0){
            for (String studentId : studentIds){
                sb.append("?,");
            }
        }
        if(sb.length() > 1){
            sql = sql + sb.substring(0, sb.length() - 1);
        }
        sql = sql + ")";
        int rowId = -1;
        rowId = ConnectionUtil.DML(sql, studentIds);
        return rowId;
    }

    @Override
    public List<Student> queryStudentByIdAndName(String queryCode, String queryName) {
        String sql = "select id,name,gender,birthDay,email,remark\n" +
                "from student " +
                "where id like ? and name like ?";
        List<Student> students = new ArrayList<>();
        ConnectionUtil.DQL(sql, new Object[]{queryCode, queryName}, rs -> {
            try{
                while (rs.next()){
                    Student student = new Student();
                    tableToPojo(student, rs, new Integer[]{1,2,3,4,5,6});
                    students.add(student);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        return students;
    }

    private List<Student>fromResultGetList(Object obj1, Object obj2, String sql){
        List<Student> students =new ArrayList<>();
        ConnectionUtil.DQL(sql, new Object[]{obj1, obj2}, rs -> {
            try{
                while (rs.next()){
                    Student student = new Student();
                    tableToPojo(student, rs, new Integer[]{1,2,3,4,5,6});
                    students.add(student);
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        });
        return students;
    }

    public static void tableToPojo(Student student, ResultSet rs, Integer[] resultIndex) throws SQLException{
        String studentId = rs.getString(resultIndex[0]);
        String name = rs.getString(resultIndex[1]);
        String gender = rs.getString(resultIndex[2]);
        Date birthDay = rs.getDate(resultIndex[3]);
        String email = rs.getString(resultIndex[4]);
        String remark = rs.getString(resultIndex[5]);
        student.setId(studentId);
        student.setName(name);
        student.setGender(gender);
        student.setBirthDay(birthDay);
        student.setEmail(email);
        student.setRemark(remark);
    }
}
